package com.bjbn.controller;

import com.bjbn.dto.DataCleanResult;
import com.bjbn.service.BridgeInfoHistoryService;
import com.bjbn.service.BridgeAirConditioningInfoHistoryService;
import com.bjbn.service.BridgePowerInfoHistoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 数据清理控制器
 * 提供手动触发数据清理的接口，用于测试和紧急清理
 */
@Slf4j
@RestController
@RequestMapping("/api/data-clean")
public class DataCleanController {

    @Autowired
    private BridgeInfoHistoryService bridgeInfoHistoryService;

    @Autowired
    private BridgeAirConditioningInfoHistoryService bridgeAirConditioningInfoHistoryService;

    @Autowired
    private BridgePowerInfoHistoryService bridgePowerInfoHistoryService;

    /**
     * 手动触发登机桥信息历史数据清理
     * @return 清理结果
     */
    @PostMapping("/bridge-info-history")
    public Map<String, Object> cleanBridgeInfoHistory() {
        Map<String, Object> result = new HashMap<>();
        try {
            log.info("手动触发登机桥信息历史数据清理");
            DataCleanResult cleanResult = bridgeInfoHistoryService.cleanExpiredDataWithResult();
            result.put("success", cleanResult.isSuccess());
            result.put("message", cleanResult.getMessage());
            result.put("data", cleanResult);
        } catch (Exception e) {
            log.error("手动触发登机桥信息历史数据清理失败", e);
            result.put("success", false);
            result.put("message", "登机桥信息历史数据清理任务执行失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * 手动触发飞机空调信息历史数据清理
     * @return 清理结果
     */
    @PostMapping("/air-conditioning-history")
    public Map<String, Object> cleanAirConditioningHistory() {
        Map<String, Object> result = new HashMap<>();
        try {
            log.info("手动触发飞机空调信息历史数据清理");
            DataCleanResult cleanResult = bridgeAirConditioningInfoHistoryService.cleanExpiredDataWithResult();
            result.put("success", cleanResult.isSuccess());
            result.put("message", cleanResult.getMessage());
            result.put("data", cleanResult);
        } catch (Exception e) {
            log.error("手动触发飞机空调信息历史数据清理失败", e);
            result.put("success", false);
            result.put("message", "飞机空调信息历史数据清理任务执行失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * 手动触发电源信息历史数据清理
     * @return 清理结果
     */
    @PostMapping("/power-info-history")
    public Map<String, Object> cleanPowerInfoHistory() {
        Map<String, Object> result = new HashMap<>();
        try {
            log.info("手动触发电源信息历史数据清理");
            DataCleanResult cleanResult = bridgePowerInfoHistoryService.cleanExpiredDataWithResult();
            result.put("success", cleanResult.isSuccess());
            result.put("message", cleanResult.getMessage());
            result.put("data", cleanResult);
        } catch (Exception e) {
            log.error("手动触发电源信息历史数据清理失败", e);
            result.put("success", false);
            result.put("message", "电源信息历史数据清理任务执行失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * 手动触发所有历史数据清理
     * @return 清理结果
     */
    @PostMapping("/all-history")
    public Map<String, Object> cleanAllHistory() {
        Map<String, Object> result = new HashMap<>();
        try {
            log.info("手动触发所有历史数据清理");
            
            // 清理登机桥信息历史数据
            DataCleanResult bridgeResult = bridgeInfoHistoryService.cleanExpiredDataWithResult();
            
            // 清理飞机空调信息历史数据
            DataCleanResult airConditioningResult = bridgeAirConditioningInfoHistoryService.cleanExpiredDataWithResult();
            
            // 清理电源信息历史数据
            DataCleanResult powerResult = bridgePowerInfoHistoryService.cleanExpiredDataWithResult();
            
            boolean allSuccess = bridgeResult.isSuccess() && airConditioningResult.isSuccess() && powerResult.isSuccess();
            result.put("success", allSuccess);
            result.put("message", allSuccess ? "所有历史数据清理任务执行成功" : "部分历史数据清理任务执行失败");
            
            Map<String, DataCleanResult> data = new HashMap<>();
            data.put("bridgeInfoHistory", bridgeResult);
            data.put("airConditioningHistory", airConditioningResult);
            data.put("powerInfoHistory", powerResult);
            result.put("data", data);
            
        } catch (Exception e) {
            log.error("手动触发所有历史数据清理失败", e);
            result.put("success", false);
            result.put("message", "历史数据清理任务执行失败: " + e.getMessage());
        }
        return result;
    }
}
